<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css">
<div id="pad-wrapper" class="new-user">

	<div class="row-fluid header">
		<h3>编辑</h3>
	</div>
	<div class="row-fluid form-wrapper">
		<!-- left column -->
		<div class="span9 with-sidebar">
			<div class="container">
				<form class="new_user_form form-horizontal" action="/knowledge/save" method="post" id="dataForm" enctype="multipart/form-data">
		                    <input type="hidden" name="id" value="<?php echo $knowledges['id']?>">
                                    <input type="hidden" name="type" value="<?php echo $type?>">
                                    <div class="field-box"></div>
					<div class="control-group form-group span12">
						<label class="control-label" for="title">主标题:</label>
						<div class="controls col-lg-5">
                                                    <input type="text" class="form-control" id="title" name="title" placeholder="主标题" value="<?php echo $knowledges['title']?>"/>
						</div>
					</div>
                                        <div class="control-group form-group span12">
						<label class="control-label" for="teacher">讲师:</label>
						<div class="controls col-lg-5">
                                                    <select id ="teacher_id" name="teacher_id">
                                                        <option value="0">请选择</option>
								<?php foreach ($teachers as $teacher):?>
								<option  value="<?php echo $teacher['teacher_id']?>"
                                                                          <?php if($teacher['teacher_id'] == $knowledges['teacher_id']){
                                                                              echo $selected = "selected='selected'";
                                                                          }else{
                                                                               echo  $selected = "";
                                                                          }?>>
                                                                          <?php echo $teacher['teacher_name']?></option>
								<?php endforeach;?>
                                                       </select>
						</div>
					</div>
                                        <div class="control-group form-group span12">
						<label class="control-label" for="content">副标题:</label>
                                                <div class="controls col-lg-5">
                                                    <input type = 'text' name ="content" id="content" class="form-control" placeholder="副标题" value="<?php echo $knowledges['content']?>" />
						</div>
					</div>
                                        <div class="control-group form-group span12">
						<label class="control-label" for="image">图片:</label>
                                                <div class="controls col-lg-5" id="img">
                                                    <img src="<?php echo $knowledges['image']?>"/>
						</div>
						<div class="controls col-lg-5" style="position:relative;z-index:200;">
                                                    <input class="form-control" id="image" type="file" accept="image/jpg,image/jpeg,image/png" name="image" value="重新选择图片"/>
						</div>
                                                <div class="controls col-lg-5" style="margin-top: -40px;position:relative;z-index:100;">
                                                    <input style="visibility:hidden" class="form-control" id="imageValidetor" value="1" name="imageValidetor"/>
						</div>
					</div>
					<div class="control-group form-group span12">
						<label class="control-label">分类:</label>
						<div class="controls col-lg-5">
                                                        <select id="change" name ="category_id">
								<option>请选择</option>
								<?php foreach ($categorys as $category):?>
								<option value="<?php echo $category['cate_id']?>" <?php if($category['cate_id'] == $knowledges['category_id']):?> selected="selected" <?php endif;?>><?php echo $category['cate_name']?></option>
								<?php endforeach;?>
							</select>
						</div>
					</div>

                                        <div class="control-group form-group span12">
                                        <label class="control-label" for = "tag1">分类标签：</label>
                                         <div class="controls col-lg-5">
                                             <select id="tag1_id" name="tag1_id[]" class="selectpicker show-tick" multiple data-live-search="false">                                                   
                                                       <?php foreach ($rows1 as $row1):?>                                           
                                                                      <option value="<?php echo $row1['tag_id']?>"
                                                                            <?php 
                                                                             foreach ($tags1 as $tag1){
                                                                             if($row1['tag_id'] == $tag1['relation_tag_id'] and $tag1['relation_status'] == 1):?> selected="selected" <?php endif;}?>>                                                                  
                                                                            <?php echo $row1['tag_name']?></option>
                                                       <?php endforeach;?>
                                             </select>
                                         </div>
                                       </div>
                                    
                                    <div class="control-group form-group span12">
                                        <label class="control-label" for = "tag2">普通标签：</label>
                                         <div class="controls col-lg-5">
                                             <select id="tag2_id" name="tag2_id[]" class="selectpicker show-tick" multiple data-live-search="false">                                                   
                                                       <?php foreach ($rows2 as $row2):?>                                           
                                                                      <option value="<?php echo $row2['tag_id']?>"
                                                                            <?php 
                                                                             foreach ($tags2 as $tag2){
                                                                             if($row2['tag_id'] == $tag2['relation_tag_id'] and $tag2['relation_status'] == 1):?> selected="selected" <?php endif;}?>>                                                                  
                                                                            <?php echo $row2['tag_name']?></option>
                                                       <?php endforeach;?>
                                             </select>
                                         </div>
                                       </div>
                                        
                                        <div class="control-group form-group span12">
						<label class="control-label" for="link">内容连接:</label>
						<div class="controls col-lg-5">
                                                    <input type="text" class="form-control span8" name="link" placeholder="http://" value="<?php echo $knowledges['link']?>"/>
						</div>
					</div>
                                        
                                         <div class="control-group form-group span12">
						<label class="control-label" for="start_time">课程开始时间:</label>
                                                <div class="controls col-lg-5">
                                                    <input type="text"name="start_time" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" value="<?php echo $knowledges['start_time']?>"></input>					
                                                </div>
                                        </div>
                                        <div class="control-group form-group span12">
						<label class="control-label" for="end_time">课程结束时间:</label>
						<div class="controls col-lg-5">
                                                    <input type="text" name="end_time" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" value="<?php echo $knowledges['end_time']?>"/>
						</div>
					</div>
                                        
                                        <div class="control-group form-group span12">
						<label class="control-label" for="is_top">置顶:</label>
						<div class="controls col-lg-5">
                                                    <input type="radio" class="form-control" name="is_top" value="1" id="is_top" onclick="gd()" <?php if($knowledges['is_top'] == 1){ echo "checked";}?>/>是
                                                    <input type="radio" class="form-control" name="is_top" value="0" id="is_top" onclick="gd()" <?php if($knowledges['is_top'] == 0){ echo "checked";}?>/>否
						</div>
					</div>
                                         <div class="control-group form-group span12">
						<label class="control-label" for="t_sort">置顶序号:</label>
						<div class="controls col-lg-5">
                                                    <input type="text" class="form-control"  id="t_sort" name="t_sort" value="<?php echo $knowledges['t_sort']?>"
                                                        <?php if($knowledges['is_top'] == 0){
                                                              echo  "disabled = 'disabled'";                                                   
                                                            }?>/>
						</div>
					</div>
                                         <div class="control-group form-group span12">
						<label class="control-label" for="ys_view_count">预设浏览数:</label>
						<div class="controls col-lg-5">
                                                    <input type="text" class="form-control" name="ys_view_count" value="<?php echo $knowledges['ys_view_count']?>" />
						</div>
					</div>
                                        <div class="control-group form-group span12">
						<label class="control-label" for="view_count">用户浏览数:</label>
						<div class="controls col-lg-5">
                                                    <?php echo $knowledges['view_count']?>
						</div>
					</div>
			
					<div class="span12 field-box"></div>
					<div class="control-group form-group span12 actions">
						<label class="control-label" for="" class="control-label"></label>
						<div class="controls col-lg-5">
							<input type="submit" class="btn-glow primary" value="提交" />
                                                        <input type="reset" class="btn-glow primary" value="取消" />
						</div>
					</div>
				</form>
			</div>
		</div>
		<!-- side right column -->
		<div class="span3 form-sidebar pull-right">
		</div>
	</div>	
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>
    <script type="text/JavaScript" src="/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript">
	$(document).ready(function() {
                $(".selectpicker").selectpicker({noneSelectedText:'请选择'});
		$('#dataForm').bootstrapValidator({
			message: 'This value is not valid',
			feedbackIcons: {
				valid: 'glyphicon glyphicon-ok',
				invalid: 'glyphicon glyphicon-remove',
				validating: 'glyphicon glyphicon-refresh'
			},
			fields: {
				title: {
					message: '主标题不符合规则',
					validators: {
						notEmpty: {
							message: '主标题是必填项，不能为空'
						}
					}
				},
                                content: {
					message: '副标题不符合规则',
					validators: {
						notEmpty: {
							message: '副标题是必填项，不能为空'
						}
					}
				},
                                category_id: {
					message: '分类不符合规则',
					validators: {
						notEmpty: {
							message: '分类是必填项,不能为空'
						},
                                                regexp: {
							regexp: /\d/,
							message: '请选择分类'
						}
					}
                                    },
				change: {
					message: '课程分类不符合规则',
					validators: {
						notEmpty: {
							message: '课程分类是必填项，不能为空'
						}
					}
				},
                                imageValidetor: {
					message: '图片不符合规则',
					validators: {
						notEmpty: {
							message: '图片是必填项，不能为空'
						}
					}
				},
                                tag1_id: {
					message: '分类标签不符合规则',
					validators: {
						notEmpty: {
							message: '分类标签是必填项，不能为空'
						}
					}
				},
                                tag2_id: {
					message: '普通标签不符合规则',
					validators: {
						notEmpty: {
							message: '普通标签是必填项，不能为空'
						}
					}
				},
                                link: {
					message: '内容链接不符合规则',
					validators: {
						notEmpty: {
							message: '内容链接是必填项，不能为空'
						}
					}
				},
                                start_time: {
					message: '开始时间不符合规则',
					validators: {
						notEmpty: {
							message: '开始时间是必填项，不能为空'
						}
					}
				},
                                end_time: {
					message: '结束时间不符合规则',
					validators: {
						notEmpty: {
							message: '结束时间是必填项，不能为空'
						}
					}
				},                              
			}
		});

		               
                $("#change").change(function(e){
			if($(this).val())
			{
				$.ajax({
					url: "/knowledge/ajaxtags",
					type: "post",
					data: {"category_id":  $(this).val()},
					dataType: "json",
					success: function(res)
					{
						var option = "";
						$.each(res, function(i, val){
							option += "<option value='" +  val["tag_id"]  + "'>";
							option += val["tag_name"];
							option += "</option>";
							
						});
						$("#tag1_id").html("");
						$("#tag1_id").html(option);

						//bootstrapt-select 增加代码 不然无法动态显示后加载数据
						$(".selectpicker").selectpicker('refresh');
						$(".selectpicker").selectpicker('show');
					}
				});
			}
				
		});
                
                $("#change").change(function(e){
			if($(this).val())
			{
				$.ajax({
					url: "/knowledge/ajaxtag",
					type: "post",
					data: {"category_id":  $(this).val()},
					dataType: "json",
					success: function(res)
					{
						var option = "";
						$.each(res, function(i, val){
							option += "<option value='" +  val["tag_id"]  + "'>";
							option += val["tag_name"];
							option += "</option>";
							
						});
						$("#tag2_id").html("");
						$("#tag2_id").html(option);

						//bootstrapt-select 增加代码 不然无法动态显示后加载数据
						$(".selectpicker").selectpicker('refresh');
						$(".selectpicker").selectpicker('show');
					}
				});
			}
				
		});
                
                 $("#teacher_id").change(function(e){
			if($(this).val())
			{
				$.ajax({
					url: "/knowledge/ajaxcontent",
					type: "post",
					data: {"teacher_id":  $(this).val()},
					dataType: "json",
					success: function(res)
					{ 
                                            if(res.length>0){
                                                $("#content").val(res[0].title);  
                                                var img= "<img id ='image' src ='"+ res[0].teacher_image + "'/>";
                                                $("#imageValidetor").val("1");
                                                $("#img").html(img);                                     
                                            }else{
                                                 $("#content").attr("value","");
                                                 $("#content").val("");
                                                 $("#img").html("");
                                                 $("#imageValidetor").val("");
                                            }
                                            $(dataForm).data("bootstrapValidator").resetForm();
                                            $(dataForm).data("bootstrapValidator").validate();
						                                              
					}
				});
			}
				
		});
                
                 $("#image").change(function(e){
			if($(this).val()!="")
			{   
                            $("#imageValidetor").val("1");
			}else{
                            $("#imageValidetor").val("");
                        }
                        $(dataForm).data("bootstrapValidator").resetForm();
                        $(dataForm).data("bootstrapValidator").validate();
		});
    });
	</script>
        <script type="text/javascript">  
            function gd() {  
                var value = document.getElementById("is_top").checked;  
                if (!value) {  
                    document.getElementById("t_sort").disabled = true;  
                } else {  
                    document.getElementById("t_sort").disabled = false;  
                }  
            }  

       </script>  
</div>